local guijue = fk.CreateSkill{
  name = "wk_heg__guijue",
}

local H = require "packages/ol_hegemony/util"

Fk:loadTranslationTable{
  ["wk_heg__guijue"] = "归绝",
  [":wk_heg__guijue"] = "当你进入濒死状态时，你可以摸两张牌，然后于此濒死结算中，对大势力角色的求【桃】询问最先执行，且与你势力不同的角色可以获得你两张手牌以视为对你使用【桃】。",
  
  ["#wk_heg__guijue-invoke"] = "归绝：是否获得 黄权 两张手牌，视为对其使用【桃】",
  ["#wk_heg__guijue_ask"] = "归绝：请使用【桃】或【酒】",
  ["@@wk_heg__guijue"] = "归绝",

  ["$wk_heg__guijue1"] = "归蜀无路，孤臣泪尽江北。",
  ["$wk_heg__guijue2"] = "受吾主殊遇，安能降吴！",
}

guijue:addEffect(fk.EnterDying, {
  can_trigger = function (self, event, target, player, data)
    return player == target and player:hasSkill(guijue.name)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(2, guijue.name)
    room:setPlayerMark(player, "@@wk_heg__guijue", 1)
    local ps = table.filter(room.alive_players, function (p) return H.isBigKingdomPlayer(p) end)
    room:sortByAction(ps)
    local smalls = table.filter(room.alive_players, function (p) return not H.isBigKingdomPlayer(p) end)
    room:sortByAction(smalls)
    table.insertTable(ps, smalls)
    event:setCostData(self, {ps = ps})
  end,

  late_refresh = true,
  can_refresh = function (self, event, target, player, data)
    return player == target and player:getMark("@@wk_heg__guijue") > 0 and event:getCostData(self) and event:getCostData(self).ps
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local logic = room.logic
    if player.hp < 1 then
    -- room.logic:trigger(fk.Dying, dyingPlayer, dyingStruct)
      local savers = event:getCostData(self).ps
      for _, p in ipairs(savers) do
        if not p.dead then
          if player.hp > 0 or player.dead or logic:trigger(fk.AskForPeaches, p, data) then
            break
          end
        end
      end
      logic:trigger(fk.AskForPeachesDone, player, data)
    end
    room:setPlayerMark(player, "@@wk_heg__guijue", 0)
  end,
})

-- guijue:addEffect(fk.EnterDying, {
--   can_refresh = function (self, event, target, player, data)
--     return player == target and player:hasShownSkill(guijue.name)
--   end,
--   on_refresh = function (self, event, target, player, data)
--     local room = player.room
--     for _, p in ipairs(room.alive_players) do
--       if not H.isBigKingdomPlayer(p) then
--         room:setPlayerMark(p, "wk_heg__guijue_sequence", 1)
--       end
--     end
--   end,
-- })

guijue:addEffect(fk.AskForCardUse, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if target ~= player and player:hasSkill(guijue.name) and player:hasShownSkill(guijue.name) and data.pattern and player:getHandcardNum() > 1 
      and H.compareKingdomWith(player, target, true) and player:getMark("@@wk_heg__guijue") > 0 then
      local matcherParsed = Exppattern:Parse(data.pattern)
      local peach = Fk:cloneCard("peach")
      return table.find(matcherParsed.matchers, function(matcher) return table.contains(matcher.name or {}, "peach") or table.contains(matcher.trueName or {}, "peach") end)
        and matcherParsed:match(peach) and data.extraData and data.extraData.must_targets 
        and table.find(data.extraData.must_targets, function(id) return id == player.id and not (target:prohibitUse(peach) and target:isProhibited(player.room:getPlayerById(id), peach)) end)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(target, {skill_name = guijue.name, prompt = "#wk_heg__guijue-invoke"}) then
      local cards = room:askToChooseCards(target,{
        target = player,
        min = 2,
        max = 2,
        flag = "h",
        skill_name = guijue.name,
      })
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self).cards
    room:obtainCard(target, cards, false, fk.ReasonPrey)
    local peach = Fk:cloneCard("peach")
    data.result = {
      from = target,
      tos = {player},
      card = peach,
    }
      return true
    --room:useVirtualCard("peach", nil, target, player, guijue.name)
  end,
})

return guijue